<template>
	<view>
		<u-popup :show="show" :round="10" @close="close">
			<view class="popupTitle">
				<u-search :focus="focus" placeholder="请输入关键字" v-model="searchKey" style="margin: 10rpx 0;"
					@search='search' @clear="clear" @custom='search' actionText="搜索"></u-search>
			</view>
			<view class="popupBody">
				<z-paging ref="paging" style="height: 100%;" :fixed="false" v-model="data" @query="query"
					:default-page-no="1" :default-page-size="10">
					<template v-if="loading">
						<view class="userInfoBox" v-for="item in 20" :key="item">
							<view style="height: 100rpx;width: 100rpx;border-radius: 8rpx;" class="Skeleton" />
							<view class="userContent">
								<view style="height: 30rpx;" class="Skeleton" />
								<view style="height: 30rpx;" class="Skeleton" />
							</view>
						</view>

					</template>
					<template v-else>

						<template v-if="type">
							<u-checkbox-group v-model="checkboxValue1" placement="column">

								<view v-for="(item, index) in data" :key="index" class="userInfoBox"
									@click.stop="selectCheck(item)"><u-checkbox :ref='`checkbox${item.user_id}`'
										:name="item.user_id" />
									<view style="margin-left: 10rpx;">
										<u--image :fade='false' :src="item.user.avatar" width="100rpx" height="100rpx"
											radius='8rpx'></u--image>
									</view>
									<view class="userContent">
										<view>
											<u-text :lines='1' size='24rpx' :text='item.user.nick_name'></u-text>
										</view>
									</view>
								</view>
							</u-checkbox-group>
						</template>
						<u-radio-group v-else v-model="radiovalue" placement="column">
							<u-radio v-for="(item, index) in data" :key="index" :name="item.id">
								<view class="userInfoBox" @click.stop="radiovalue=item.id">
									<u--image :fade='false' :src="item.user.avatar" width="100rpx" height="100rpx"
										radius='8rpx'></u--image>
									<view class="userContent">
										<view>
											<u-text :lines='1' size='24rpx' :text='item.user.nick_name'></u-text>
										</view>
									</view>
								</view>
							</u-radio>
						</u-radio-group>

					</template>
				</z-paging>
			</view>
			<view style="margin-top: 10rpx;padding: 0 40rpx;" v-if="type">
				<u-button @click='checkUserBinding' color="#3975c6" v-if="checkboxValue1.length">
					确认
				</u-button>
				<u-button @click='checkUserBinding' v-else>
					确认
				</u-button>
			</view>
			<view style="margin-top: 10rpx;padding: 0 40rpx;" v-else>
				<u-button @click='userBinding' color="#3975c6" v-if="radiovalue">
					确认
				</u-button>
				<u-button @click='userBinding' v-else>
					确认
				</u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		props: ['show', 'form', 'type', 'member'],
		data() {
			return {
				focus: false,
				searchKey: '',
				data: [],
				loading: true,
				radiovalue: null,
				checkboxValue1: [],
				users: [],
				first: true
			}
		},
		mounted() {
			if (this.member) {
				this.users = [...this.member]
				this.checkboxValue1 = this.member.map(item => item.user_id)
			}
		},
		watch: {
			checkboxValue1(New, Old) {
				if (!this.first) {
					var differentElements = [];
					  for (var i = 0; i < New.length; i++) {
					    if (Old.indexOf(New[i]) === -1) {
					      differentElements.push(New[i]);
					    }
					  }
					
					  for (var j = 0; j < Old.length; j++) {
					    if (New.indexOf(Old[j]) === -1) {
					      differentElements.push(Old[j]);
					    }
					  }
					
					console.log(differentElements)
					differentElements.forEach(item => {
						this.selectUser(item)
					})
				}
			}
		},
		methods: {
			selectCheck(item) {
				if (this.checkboxValue1.includes(item.user_id)) {
					this.checkboxValue1 = this.checkboxValue1.filter(check => check != item.user_id)
				} else {
					this.checkboxValue1=[...this.checkboxValue1,item.user_id]
				}
			},
			selectUser(id) {
				const ids = this.users.map(item => item.user_id)
				if (ids.includes(id)) {
					this.users = this.users.filter(item => item.user_id != id)
				} else {
					const item = this.data.filter(item => item.user_id == id)[0]
					this.users.push(item)
				}

			},
			close() {
				this.$emit('close')
			},
			search() {
				this.$refs.paging.reload()
				this.radiovalue = null
			},
			clear() {
				this.focus = false
				setTimeout(() => {
					this.focus = true
				}, 100)
			},
			query(page, size) {
				let obj = {
					page,
					size,
					keywords: this.searchKey
				}
				uni.showLoading({
					title: '加载中...'
				})
				this.$api_company.Administrator.List(obj).then(res => {
					this.$refs.paging.complete(res.data.data)
					uni.hideLoading()
					this.loading = false
					this.first = false
				}).catch(err => {
					uni.$u.toast(err.message)
					uni.hideLoading()
				})

			},
			userBinding() {
				if (!this.radiovalue) {
					uni.$u.toast('请先选择审核人')
					return
				}
				const item = this.data.filter(item => item.id == this.radiovalue)[0]
				this.$emit('selectAuthorizer', {
					...item,
					id: item.user_id,
					members: item.user
				})
				this.radiovalue = null
			},
			checkUserBinding() {
				this.$emit('selectMember', this.users)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.userInfoBox {
		display: flex;
		margin-bottom: 20rpx;
		align-items: center;
		width: 100%;

		.userContent {
			margin-left: 10px;
			flex: 1;
			display: flex;
			height: 100rpx;
			flex-direction: column;
			justify-content: space-between;
			border-bottom: 1rpx solid #f1f1f1;
			padding: 0 0 10rpx 0;
		}
	}

	/deep/ uni-checkbox:not([disabled]) .uni-checkbox-input:hover {
		border-color: #d1d1d1;
	}
</style>